package com.rongke.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.rongke.commons.JsonResp;
import com.rongke.model.BuyTogetherGoods;
import com.rongke.model.GoodsDetail;
import com.rongke.model.PanicBuyingGoods;
import com.rongke.service.BuyTogetherGoodsService;
import com.rongke.service.GoodsDetailService;
import com.rongke.service.GoodsService;
import com.rongke.service.PanicBuyingGoodsService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @GoodsDetailController
 * @商品详情Controller
 * @version : Ver 1.0
 */
@CrossOrigin(origins = "*",maxAge = 3600)
@RestController
@RequestMapping(value="/api/goodsDetail")
@Transactional
public class GoodsDetailController {
    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private GoodsDetailService goodsDetailService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private PanicBuyingGoodsService panicBuyingGoodsService;
    @Autowired
    private BuyTogetherGoodsService buyTogetherGoodsService;

    /**
     * @添加商品详情
     * @param goodsDetail
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/add", method = RequestMethod.POST)
    public JsonResp addGoodsDetail(@RequestBody GoodsDetail goodsDetail){
        log.debug("添加商品详情");
        goodsDetailService.insert(goodsDetail);
        return JsonResp.ok(goodsDetail);
    }

    /**
     * @添加或修改商品详情
     * @param goodsDetail
     * @return
     */
    @RequestMapping(value = "/saveOrUpdate",method=RequestMethod.POST)
    public JsonResp saveOrUpdate(@RequestBody GoodsDetail goodsDetail){
        log.debug("添加或修改商品详情");
        if(goodsDetail.getGoodsId()==null)
            return JsonResp.toFail("没有商品id参数，出错！");
        //添加
        if(goodsDetail.getId()==null){
            goodsDetailService.insert(goodsDetail);
        }else{
            //修改
            goodsDetailService.updateById(goodsDetail);
        }
        return JsonResp.ok(goodsDetail);
    }

    /**
     * @修改商品详情
     * @param goodsDetail
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/update", method = RequestMethod.POST)
    public JsonResp updateGoodsDetail(@RequestBody GoodsDetail goodsDetail){
        log.debug("修改商品详情");
        if(null==goodsDetail.getId()&&null!=goodsDetail.getGoodsId()){
            EntityWrapper<GoodsDetail> ew=new EntityWrapper<>();
            ew.eq("goods_id",goodsDetail.getGoodsId());
            GoodsDetail temp=goodsDetailService.selectOne(ew);
            if(null==temp){
                return JsonResp.toFail("没有找到相应的商品");
            }
            goodsDetail.setId(temp.getId());
        }
        goodsDetailService.updateById(goodsDetail);
        return JsonResp.ok(goodsDetail);
    }
    /**
     * @根据id查找商品详情
     * @param id
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/selectOne", method = RequestMethod.GET)
    public JsonResp selectGoodsDetail(Long id){
        log.debug("查找商品详情");
        GoodsDetail goodsDetail = goodsDetailService.selectById(id);
        return JsonResp.ok(goodsDetail);
    }

    /**
     * @根据商品id获得商品详情
     * @param goodsDetail
     * @return
     */
    @RequestMapping(value="/selectGoodsDetailByGoodsId",method=RequestMethod.POST)
    public JsonResp selectGoodsDetailByGoodsId(@RequestBody GoodsDetail goodsDetail){
        log.debug("根据商品id获得商品详情");
        GoodsDetail gd=goodsDetailService.selectByCondition(goodsDetail);
        String detailLink="https://wx.miduu.com/6.3.0goods_detail.html?id="+gd.getGoodsId();
        Map map =new HashMap();
        map.put("goodsDetail",gd);
        map.put("currentTime",new Date());
        map.put("detailLink",detailLink);
        EntityWrapper<BuyTogetherGoods> ew=new EntityWrapper<>();
        ew.eq("goods_id",gd.getGoodsId());
        BuyTogetherGoods  btg=buyTogetherGoodsService.selectOne(ew);
        if(btg!=null){
            map.put("buyTogetherPrice",btg.getBuyTogetherPrice());
        }
        EntityWrapper<PanicBuyingGoods> ewpbg=new EntityWrapper<>();
        ewpbg.eq("goods_id",goodsDetail.getGoodsId());
        PanicBuyingGoods panicBuyingGoods=panicBuyingGoodsService.selectOne(ewpbg);
        if(panicBuyingGoods!=null){
            map.put("timeReturnRate",panicBuyingGoods.getTimeReturnRate());
        }
        return JsonResp.ok(map);
    }

    /**
     * @根据商品id获得商品详情(cheetahlou)
     * @param goodsId  商品id
     * @return
     */
    @RequestMapping(value="/getGoodsDetailByGoodsId",method=RequestMethod.GET)
    public JsonResp getGoodsDetailByGoodsId(Long goodsId){
        log.debug("根据商品id获得商品详情");
        GoodsDetail goodsDetail=new GoodsDetail();
        goodsDetail.setGoodsId(goodsId);
        GoodsDetail gd=goodsDetailService.selectByCondition(goodsDetail);
        if(null==gd)
            return JsonResp.toFail("找不到goodsId为"+goodsId+"的商品详情");
        return JsonResp.ok(gd);
    }


}
